package com.pharmacy.mapper;


import com.github.pagehelper.Page;
import com.pharmacy.dto.MedicinesPageSelectDto;
import com.pharmacy.dto.Quantity;
import com.pharmacy.entity.Medicines;
import com.pharmacy.vo.MedicinesVo;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MedicinesMapper {

    /**
     * 添加药品
     * @param medicines
     */
    @Insert("INSERT INTO medicines(name, barcode, price, stock, description, created_at, updated_at, image, isPrescription, expiration_date,categories_id) " +
            "VALUES (#{name}, #{barcode}, #{price}, #{stock}, #{description}, #{createdAt}, #{updatedAt}, #{image}, #{isPrescription}, #{expirationDate},#{categoriesId})")
    void addMedicines(Medicines medicines);


    /**
     * 根据条形码查询药品
     * @param barcode
     * @return
     */
//    @Select("select * from medicines where barcode = #{barcode}")
    MedicinesVo getMedicines(String barcode);


    /**
     * 分页查询药品
     * @param medicinesPageSelectDto
     * @return
     */
    Page<MedicinesVo> pageSelectMedicines(MedicinesPageSelectDto medicinesPageSelectDto);

    /**
     * 删除药品
     * @param id
     */
    @Delete("delete from medicines where id = #{id}")
    void deleteMedicines(Integer id);

    /**
     * 修改药品信息
     * @param medicines
     */
    void updateMedicines(Medicines medicines);


    /**
     * 查询快过期的药品
     * @param expirationDate
     * @return
     */
    @Select("select * from medicines where expiration_date < #{expirationDate}")
    List<Medicines> getExpiredDrugs(LocalDateTime expirationDate);

    /**
     * 根据id查询药品
     * @param id
     * @return
     */
    @Select("select * from medicines where id = #{id}")
    Medicines getById(Integer id);


    /**
     * 批量减少药品数量
     * @param id
     * @param quantity
     */
    @Update("update medicines set stock = stock - #{quantity} where id = #{id}")
    void batchReduceMedicine(Integer id ,Integer quantity);
}
